/* ActorRepository.java
 *
 * Copyright (C) 2013 Universidad de Sevilla
 * 
 * The use of this project is hereby constrained to the conditions of the 
 * TDG Licence, a copy of which you may download from 
 * http://www.tdg-seville.info/License.html
 * 
 */

package repositories;

import java.util.Collection;
import java.util.Date;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import domain.Event;

@Repository
public interface EventRepository extends JpaRepository<Event, Integer> {
	@Query("select e from Event e where e.startMoment>?1")
	Collection<Event> findEventsAvailable(Date currentMoment);

	@Query("select e from Event e order by e.rating DESC")
	Collection<Event> findAllOrderedByRating();
	
	@Query("select e from Event e where e.startMoment<=?1 and e.finishMoment>?1")
	Collection<Event> findEventsOngoing(Date currentMoment);

	@Query("select a.events from Administrator a where a.id = ?1")
	Collection<Event> findEventsByAdministrator(int id);

	@Query("select r.event from Participant p join p.registrations r where p.id = ?1")
	Collection<Event> findEventsByParticipant(int id);

	@Query("select e from Event e where e.startMoment<=?1 and e.finishMoment>?1 and e.administrator.id=?2")
	Collection<Event> findEventsOngoingByAdministrator(Date currentMoment, int id);

	@Query("select r.event from Registration r  where r.participant.id=?1 order by r.event.rating DESC")
	Collection<Event> findByParticipantOrderedByRating(int id);
	
	@Query("select e from Administrator a join a.events e where a.id=?1 order by e.rating DESC")
	Collection<Event> findByAdministratorOrderedByRating(int id);

}